package shard;

import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * Redis 部署是集群模式（推荐）
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2025/8/25 16:51
 * @updateUser: MI
 * @updateTime: 2025/8/25 16:51
 * @updateRemark: 修改内容
 * @version: 1.0
 */
public class ShardingTest {
    public static void main(String[] args) {

        HostAndPort hp4 = new HostAndPort("192.168.110.65",6379);
        HostAndPort hp5 = new HostAndPort("192.168.110.66",6379);
        HostAndPort hp6 = new HostAndPort("192.168.110.67",6379);

        Set nodes = new HashSet<HostAndPort>();

        nodes.add(hp4);
        nodes.add(hp5);
        nodes.add(hp6);

        // 3. 配置客户端参数（包含密码认证）
        JedisClientConfig clientConfig = DefaultJedisClientConfig.builder()
                .password("seata@user123") // 设置Redis密码
                .connectionTimeoutMillis(2000) // 连接超时时间（毫秒）
                .socketTimeoutMillis(2000)    // 读写超时时间（毫秒）
                .build();
        try (JedisCluster jedisCluster = new JedisCluster(nodes,clientConfig)) {

            for (int i = 0; i < 100; i++) {
                jedisCluster.set("k" + i, "" + i);
            }
            for (int i = 0; i < 100; i++) {
                System.out.println("取到值：" + jedisCluster.get("k" + i));
            }
        }
    }
}
